package com.bjc.platform.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;




import com.bjc.platform.model.AdmDepartmentType;
import com.bjc.platform.model.AdmOrg;
import com.bjc.platform.service.AdmOrgService;
import com.bjc.platform.service.IAdmDepartmentTypeService;
import com.bjc.platform.util.StaticMethod;

@Controller
@RequestMapping("/depType")
public class AdmDepartmentTypeController extends BaseController {

    @Autowired
    @Qualifier("admDepartmentTypeService")
    private IAdmDepartmentTypeService admDepartmentTypeService;

    @Autowired
    private AdmOrgService admOrgService;

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @ResponseBody
    public Object query() {
        try {
            return StaticMethod.TOJSONResult(true, admDepartmentTypeService.query(StaticMethod.getOperationCompany().getCode()))
                    .getJSONResult();
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/queryManufacture", method = RequestMethod.GET)
    @ResponseBody
    public Object queryManufacture() {
        try {
            
            return StaticMethod.TOJSONResult(true, admDepartmentTypeService.queryManufacture(StaticMethod.getOperationCompany().getCode()))
                    .getJSONResult();
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/queryType", method = RequestMethod.GET)
    @ResponseBody
    public Object queryType() {
        try {
            String code = StaticMethod.getOperationCompany().getCode();
            return StaticMethod.TOJSONResult(true, code).getJSONResult();
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/queryByCompany", method = RequestMethod.GET)
    @ResponseBody
    public Object queryByCompany(@RequestParam("companyId") Long companyId) {
        try {
            AdmOrg org = admOrgService.get(companyId);
            List<AdmDepartmentType> departs = new ArrayList<AdmDepartmentType>();
            AdmDepartmentType ad = new AdmDepartmentType();
            ad.setName("　");
            departs.add(ad);
            departs.addAll(admDepartmentTypeService.query(org.getCompany().getCode()));
            if(departs.size() == 1){
            	departs = null;
            }
            return StaticMethod.TOJSONResult(true, departs).getJSONResult();
        } catch (Exception e) {
            return StaticMethod.TOJSONResult(false, null).getJSONResult();
        }
    }

}
